package lab.mongo.controller;

import com.mongodb.client.result.UpdateResult;
import lab.mongo.pojo.*;
import lab.mongo.service.IMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
public class MongoController {

    @Autowired
    IMongoService mongoService;

    @RequestMapping(value = "/subject", method = RequestMethod.GET)
    @ResponseBody List<SubjectInfo> findSubject(PageInfo pageInfo, QuerySubject query) {
        return mongoService.findSubject(pageInfo, query);
    }

    @RequestMapping(value = "/subject", method = RequestMethod.POST)
    @ResponseBody String addSubjects(@RequestBody List<SubjectInfo> subjects) {
        mongoService.addSubjects(subjects);
        return "success";
    }

    @RequestMapping(value = "/subject", method = RequestMethod.PUT)
    @ResponseBody String updateSubject(@RequestBody SubjectInfo subject) {
        UpdateResult updateResult = mongoService.updateSubject(subject);
        if (updateResult.getMatchedCount() > 0)
            return "success";
        else
            return "fail";
    }

    @RequestMapping(value = "/subject", method = RequestMethod.DELETE)
    @ResponseBody String deleteSubject(QuerySubject query) {
        mongoService.deleteSubjects(query);
        return "success";
    }

    @RequestMapping(value = "/company", method = RequestMethod.GET)
    @ResponseBody List<CompanyInfo> findCompany(PageInfo pageInfo, QueryCompany query) {
        return mongoService.findCompany(pageInfo, query);
    }

    @RequestMapping(value = "/company", method = RequestMethod.POST)
    @ResponseBody String addCompanys(@RequestBody List<CompanyInfo> companys) {
        mongoService.addCompanys(companys);
        return "success";
    }
}
